package com.example.yls.demoa;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by yls on 2019/5/10.
 */

public class MyView extends View {
    public static final float RADIUS = 70f;
    private Paint paint;
    private Point currentPoint;

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(5);
        paint.setAntiAlias(true);
//        paint.setStyle(Paint.Style.STROKE);
        paint.setTextSize(50);
    }

    public MyView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //点
        canvas.drawPoint(100, 100, paint);
        //线
        canvas.drawLine(100, 100, 200, 200, paint);
        //矩形
        canvas.drawRect(100,100,300,300,paint);
        //圆角矩形
//        canvas.drawRoundRect(100,100,300,300,50,50,paint);
        //圆
        canvas.drawCircle(200,200,100,paint);
        //弧
//        canvas.drawArc(100,100,400,400,200,50,false,paint);
        //文字
        canvas.drawText("软件",500,500,paint);


//        任意路径
        Path path = new Path();
        path.moveTo(100,100);
        path.lineTo(200,200);
        path.lineTo(300,500);
        path.lineTo(400,600);
        canvas.drawPath(path,paint);


        //五角星
//        Point a = new Point(0,100);
//        Point b = new Point(300,100);
//        Point c = new Point(50,250);
//        Point d = new Point(250,250);
//        Point e = new Point(150,0);
//        Path path = new Path();
//        path.moveTo(a.x,a.y);
//        path.lineTo(b.x,b.y);
//        path.lineTo(c.x,c.y);
//        path.lineTo(e.x,e.y);
//        path.lineTo(d.x,d.y);
//        path.lineTo(a.x,a.y);
//        canvas.drawPath(path,paint);
//        canvas.drawText("五角星",40,300,paint);

        //图片镜像
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.koala);
//        canvas.drawBitmap(bitmap,0,0,paint);
//
//        Matrix matrix1 = new Matrix();
//        matrix1.setScale(-1,1);
//        matrix1.postTranslate(300,0);
//        canvas.drawBitmap(bitmap,matrix1,paint);
//
//        Matrix matrix2 = new Matrix();
//        matrix2.setScale(1,-1);
//        matrix2.postTranslate(0,225);
//        canvas.drawBitmap(bitmap,matrix2,paint);
//
//        Matrix matrix3 = new Matrix();
//        matrix3.setScale(-1,-1);
//        matrix3.postTranslate(300,225);
//        canvas.drawBitmap(bitmap,matrix3,paint);

        //图片不同角度
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.koala);
        canvas.drawBitmap(bitmap,0,0,paint);
        Matrix matrix = new Matrix();
        matrix.setRotate(90,255,255);
        canvas.drawBitmap(bitmap,matrix,paint);

        matrix.setRotate(180,255,255);
        canvas.drawBitmap(bitmap,matrix,paint);

        matrix.setRotate(270,255,255);
        canvas.drawBitmap(bitmap,matrix,paint);
        super.onDraw(canvas);

        //动画小球
        if (currentPoint == null){
           currentPoint = new Point(RADIUS, RADIUS);
            float x = currentPoint.getX();
            float y = currentPoint.getY();
            canvas.drawCircle(x, y, RADIUS, paint);

            Point startPoint = new Point(RADIUS, RADIUS);
            Point endPoint = new Point(700, 1000);

            ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
            valueAnimator.setDuration(5000);
            valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
            valueAnimator.setRepeatCount(6);

            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    currentPoint = (Point) valueAnimator.getAnimatedValue();
                    invalidate();
                }
            });
            valueAnimator.start();
        }else {

            float x = currentPoint.getX();
            float y = currentPoint.getY();
            canvas.drawCircle(x, y, RADIUS, paint);

        }

    }
}
